/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    initMasterSelecteBySelect2();

    /**
     * 格式化工程师列表列表项
     * @param {object} repo
     * @returns {string}
     */
    function formatRepoForMaster(repo) {
        if (repo.loading) {
            return '查询中...'
        }
        var name = repo.text + (repo.disabled ? '（离职）' : '');
        return '<div><div><i class="zmn-icon zmn-icon-worker"></i><span>' + name + '</span></div><div><i class="zmn-icon zmn-icon-phone"></i><span>' + repo.ext1 + '</span></div></div>';
    }

    /**
     * 使用 初始化工程师下拉框
     */
    function initMasterSelecteBySelect2() {
        loadSelect2("select-yeyx-master", 1);
        loadSelect2("select-zmn-master", 2);
        loadSelect2("select-saas-master", 3);
        loadSelect2("select-cnhb-master", 4);
    }

    /**
     * 描述:初始化工程师下拉框
     *
     * @author wangjie
     * @since  2019/9/23
     **/
    function loadSelect2(selectId, plat) {
        $('#' + selectId).select2({
            theme: "layui",
            language: 'zh-CN',
            placeholder: '请输入工程师姓名',
            allowClear: true,
            minimumInputLength: 1,
            dropdownAutoWidth: true,
            ajax: {
                url: '/mast/master/findMasterData.action',
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    let query = {
                        realname: params.term,
                        plat: plat
                    };
                    return query;
                },
                processResults: function (response) {
                    let results = [];
                    if (response.status === 200) {
                        results = response.data;
                    }
                    results = results.map((item) => {
                        item.id = item.value;
                        return item;
                    });
                    return {
                        results: results
                    };
                }
            },
            escapeMarkup: function (markup) {
                return markup;
            },
            templateResult: formatRepoForMaster,
        });
    }

    // 点击保存
    $(document).on('click', '#submit', function () {

        let arr = [];
        let zmnMasterId = $("#select-zmn-master").val();
        if (!zmnUtil.isEmptyNull(zmnMasterId)) {
            let zmnData = {masterId: zmnMasterId, plat: 2};
            arr.push(zmnData);
        }
        let yeyxMasterId = $("#select-yeyx-master").val();
        if (!zmnUtil.isEmptyNull(yeyxMasterId)) {
            let yeyxData = {masterId: yeyxMasterId, plat: 1};
            arr.push(yeyxData);
        }
        let saasMasterId = $("#select-saas-master").val();
        if (!zmnUtil.isEmptyNull(saasMasterId)) {
            let saasData = {masterId: saasMasterId, plat: 3};
            arr.push(saasData);
        }
        let cnhbMasterId = $("#select-cnhb-master").val();
        if (!zmnUtil.isEmptyNull(cnhbMasterId)) {
            let cnhbData = {masterId: cnhbMasterId, plat: 4};
            arr.push(cnhbData);
        }

        zmnLayUtil.ajaxSimpleUE("/mast/master/bindingStaffAndMaster.action", {
            masterListStr: JSON.stringify(arr),
            staffId: json.staffId
        }, 0, function (json) {
            if (json.statusBool) {
                zmnLayUtil.closeThisIframeMsg(parent, json.message, "");
            }else {
                layer.msg(res.message, {"icon": 2});
            }
        });


        console.log(arr);
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        zmnLayUtil.closeThisIframe(parent);
    });

    //对外暴露的接口
    exports('staff_binding_engineer', {});
});